﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace IPC.Communication.Framework.BasicFramework
{
    public static class SoftSqlOperate
    {
        public static int ExecuteSql(string conStr, string cmdStr)
        {
            int result;
            using (SqlConnection sqlConnection = new SqlConnection(conStr))
            {
                sqlConnection.Open();
                result = SoftSqlOperate.ExecuteSql(sqlConnection, cmdStr);
            }
            return result;
        }

        public static int ExecuteSql(SqlConnection conn, string cmdStr)
        {
            int result;
            using (SqlCommand sqlCommand = new SqlCommand(cmdStr, conn))
            {
                result = sqlCommand.ExecuteNonQuery();
            }
            return result;
        }

        public static DataTable ExecuteSelectTable(string conStr, string cmdStr)
        {
            DataTable result;
            using (SqlConnection sqlConnection = new SqlConnection(conStr))
            {
                sqlConnection.Open();
                result = SoftSqlOperate.ExecuteSelectTable(sqlConnection, cmdStr);
            }
            return result;
        }

        public static DataTable ExecuteSelectTable(SqlConnection conn, string cmdStr)
        {
            DataTable result;
            using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmdStr, conn))
            {
                using (DataSet dataSet = new DataSet())
                {
                    sqlDataAdapter.Fill(dataSet);
                    result = dataSet.Tables[0];
                }
            }
            return result;
        }

        public static List<T> ExecuteSelectEnumerable<T>(string conStr, string cmdStr) where T : ISqlDataType, new()
        {
            List<T> result;
            using (SqlConnection sqlConnection = new SqlConnection(conStr))
            {
                sqlConnection.Open();
                result = SoftSqlOperate.ExecuteSelectEnumerable<T>(sqlConnection, cmdStr);
            }
            return result;
        }

        public static List<T> ExecuteSelectEnumerable<T>(SqlConnection conn, string cmdStr) where T : ISqlDataType, new()
        {
            List<T> result;
            using (SqlCommand sqlCommand = new SqlCommand(cmdStr, conn))
            {
                using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
                {
                    List<T> list = new List<T>();
                    while (sqlDataReader.Read())
                    {
                        T item = Activator.CreateInstance<T>();
                        item.LoadBySqlDataReader(sqlDataReader);
                        list.Add(item);
                    }
                    result = list;
                }
            }
            return result;
        }

        public static T ExecuteSelectObject<T>(string conStr, string cmdStr) where T : ISqlDataType, new()
        {
            T result;
            using (SqlConnection sqlConnection = new SqlConnection(conStr))
            {
                sqlConnection.Open();
                result = SoftSqlOperate.ExecuteSelectObject<T>(sqlConnection, cmdStr);
            }
            return result;
        }

        public static T ExecuteSelectObject<T>(SqlConnection conn, string cmdStr) where T : ISqlDataType, new()
        {
            T result;
            using (SqlCommand sqlCommand = new SqlCommand(cmdStr, conn))
            {
                using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
                {
                    bool flag = sqlDataReader.Read();
                    if (flag)
                    {
                        T t = Activator.CreateInstance<T>();
                        t.LoadBySqlDataReader(sqlDataReader);
                        result = t;
                    }
                    else
                    {
                        result = default(T);
                    }
                }
            }
            return result;
        }

        public static int ExecuteSelectCount(string conStr, string cmdStr)
        {
            int result;
            using (SqlConnection sqlConnection = new SqlConnection(conStr))
            {
                sqlConnection.Open();
                result = SoftSqlOperate.ExecuteSelectCount(sqlConnection, cmdStr);
            }
            return result;
        }

        public static int ExecuteSelectCount(SqlConnection conn, string cmdStr)
        {
            int result;
            using (SqlCommand sqlCommand = new SqlCommand(cmdStr, conn))
            {
                int num = 0;
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                bool flag = sqlDataReader.Read();
                if (flag)
                {
                    num = Convert.ToInt32(sqlDataReader[0]);
                }
                sqlDataReader.Close();
                result = num;
            }
            return result;
        }
    }
}
